//
//    AppSourceThread.h
//    Class for managing sources and the threads they live in
//    2006 John Wiggins
//    Based on code from Jeepers Elvis, 2000 John Wiggins
//
// This program is free software; you can redistribute it and/or modify  
// it under the terms of the GNU General Public License as published by  
// the Free Software Foundation; either version 2 of the License, or     
// (at your option) any later version.                                   
// 
// This program is distributed in the hope that it will be useful,       
// but WITHOUT ANY WARRANTY; without even the implied warranty of        
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
// GNU General Public License for more details.                          
//     
//     You should have received a copy of the GNU General Public License     
//     along with this program; if not, write to the                         
//         Free Software Foundation, Inc.,                                       
//         59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
//
//-------------------------------------------------------------
#ifndef APP_SOURCE_THREAD_H
#define APP_SOURCE_THREAD_H
//-------------------------------------------------------------
#include <vector>
#include <list>

#include "Ipc/IpcPort.h"
#include "App/AppPlugin.h"
#include "Ipc/IpcMessage.h"
//-------------------------------------------------------------
class AppSourceThread
{
public:
                    AppSourceThread     ();
                    ~AppSourceThread    ();
    
    RenSurface *    getSurface          ();
    bool            lockSurface         ();
    void            unlockSurface       ();
    
    ipc_port_t      portId              () const;
    status_t        postMessage         ( IpcMessage *msg ) const;
    
private:
    void            receiveMessages     ();
    void            handleMessage       ( IpcMessage *msg );

    void            addFilter           ( ge_filter_plugin *filt, ge_filter_plugin *before );
    void            removeFilter        ( ge_filter_plugin *filt );

    void            connectOutput       ( ge_output_plugin *out );
    void            disconnectOutput    ( ge_output_plugin *out );

    static void *   start_loop          ( void *data );
    void *          main_loop           ();
    
    
    std::list<ge_filter_plugin *>   mFiltList;
    std::vector<RenSurface *>       mSurfacePool;
    unsigned int                    mWorkSurface;
    unsigned int                    mScratchSurface;
    unsigned int                    mOutputSurface;
    pthread_t                       mThread;
    ipc_port_t                      mMsgPort;
    ge_source_plugin *              mSrcPlugin;
    ge_output_plugin *              mOutPlugin;
    bool                            mQuitting;
};
//-------------------------------------------------------------
bool
AppSourceThread::lockSurface()
{
    return mSurfacePool[mWorkSurface]->lock();
}
//-------------------------------------------------------------
void
AppSourceThread::unlockSurface()
{
    mSurfacePool[mWorkSurface]->unlock();
}
//-------------------------------------------------------------
ipc_port_t
AppSourceThread::portId() const
{
    return mMsgPort;
}
//-------------------------------------------------------------
status_t
AppSourceThread::postMessage( IpcMessage *msg ) const
{
    return msg->SendMessage(mMsgPort);
}
//-------------------------------------------------------------
#endif // APP_SOURCE_THREAD_H
